import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/components/Login.vue'
import Register from '@/components/Register.vue'
import AppLayout from '@/App.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
         path: '/item/edit/:id?', // 支持可选参数
         name: 'ItemEdit',
         component: () => import('@/components/ItemEditor.vue'),
         props: route => ({
         // 同时处理params和query
         id: route.params.id || route.query.id,
         type: route.query.type
          }),
         meta: { requiresAuth: true }
    },
    {
      path: '/admin/dashboard',
      name: 'AdminDashboard',
      component: () => import('@/views/AdminDashboard.vue'),
      meta: { 
        requiresAuth: true,
        allowedRoles: ['ADMIN']
      },
      children: [
        
        {
          path: '/home',
          component: () => import('@/views/Home.vue'),
          meta: { title: '控制台首页' }
        },
        {
          path: '/profile',
          component: () => import('@/components/Profile.vue'),
          meta: { title:'个人中心'}
        },
        {
          path: '/users',
          component: () => import('@/components/User.vue'),
          meta: { title:'用户管理'}
        },
        {
          path: '/found',
          component: () => import('@/components/Found.vue'),
          meta: { title:'招领信息管理'}
        },
        {
          path: '/claims',
          component: () => import('@/components/Lost.vue'),
          meta: { title:'失物信息管理'}
        },
        
        {
          path: '/statistics',
          component: () => import('@/components/Statistics.vue'),
          meta: { requiresAuth: true, role: 'ADMIN' }
        }
      ]
    },
    {
      path: '/user/dashboard',
      name: 'UserDashboard',
      component: () => import('@/views/UserDashboard.vue'),
      meta: { 
        requiresAuth: true,
        allowedRoles: ['USER'] // 允许普通用户访问
      },
      children:[
        {
          path: '/usehome',
          component: () => import('@/components/UseHome.vue'),
          meta: { title: '控制台首页' }
        },
        {
        path:'/lost',
        component: () => import('@/components/UseLost.vue')
        },
        {
          path:'/usefound',
          component: ()=> import('@/components/UseFound.vue')
        },
        {
          path:'/useprofile',
          component: () =>import('@/components/Profile.vue')
        },
        {
          path: '/my-lost',
          component: () => import('@/components/MyLost.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/my-found',
          component: () => import('@/components/MyFound.vue'),
          meta: { requiresAuth: true }
        },
        {
      path: 'notifications',
      component: () => import('@/components/NotificationList.vue'),
      meta: { 
        title: '我的通知',
        requiresAuth: true 
        }
      }
      ]
    }
  ],
})
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  const isLoginPage = to.path === '/login'
  const isRegisterPage = to.path === '/register'

  // 如果访问的是登录/注册页且已登录，重定向到首页
  if ((isLoginPage || isRegisterPage) && isAuthenticated) {
    next(localStorage.getItem('role') === 'ADMIN' ? '/admin/dashboard' : '/user/dashboard')
  }
  // 如果访问的是需要认证的页面且未登录，跳转到登录页
  else if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  }
  // 其他情况正常放行
  else {
    next()
  }
})
export default router
